package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.constant.SuperConstant;
import com.zzyl.dto.RoleDto;
import com.zzyl.entity.Role;
import com.zzyl.entity.RoleResource;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.RoleMapper;
import com.zzyl.mapper.RoleResourceMapper;
import com.zzyl.mapper.UserRoleMapper;
import com.zzyl.service.RoleService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.RoleVo;
import org.assertj.core.util.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourceMapper roleResourceMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    /**
     * 角色管理
     *
     * @param roleDto
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageResponse<RoleVo> list(RoleDto roleDto, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Role> page = roleMapper.list(roleDto.getRoleName());
        return PageResponse.of(page, RoleVo.class);
    }

    /**
     * 角色添加
     *
     * @param roleDto
     * @return
     */
    @Override
    public Boolean add(RoleDto roleDto) {
        Role bean = BeanUtil.toBean(roleDto, Role.class);
        bean.setUpdateTime(LocalDateTime.now());
        bean.setCreateTime(LocalDateTime.now());
        bean.setRemark(roleDto.getRoleName());
        bean.setCreateBy(1671403094153375879L);
        return roleMapper.insert(bean);
    }

    /**
     * 根据角色id查询资源列表
     *
     * @param roleId
     * @return
     */
    @Override
    public List<RoleVo> findCheckedResources(Long roleId) {
        return roleResourceMapper.selectResourceNoByRoleId(roleId);
    }

    /**
     * 删除角色
     *
     * @param roleIds
     * @return
     */
    @Override
    public void deleteRoleById(String roleIds) {
        //1.根据id查询角色信息 判断状态 如果是禁用则删除 如果不是则报错
        Role role = roleMapper.selectById(roleIds);
        if (null != role && SuperConstant.DATA_STATE_0.equals(role.getDataState())) {
            throw new BaseException(BasicEnum.PANRENT_ROLE_STATUS);
        }
        //2.删除角色相关的资源
        roleResourceMapper.deleteByRoleId(roleIds);
        //3.删除角色信息
        roleMapper.delete(roleIds);
    }

    /**
     * 角色修改
     *
     * @param roleDto
     * @return
     */
    @Override
    public Boolean updateRole(RoleDto roleDto) throws CloneNotSupportedException {
        Role bean = roleDto.clone();
        bean.setUpdateTime(LocalDateTime.now());
        bean.setCreateTime(LocalDateTime.now());
        bean.setUpdateBy(1671403256519078138L);

        //状态的修改 如果是修改为禁用 那么需要查询当前角色下是否有用户
        if (null != roleDto && SuperConstant.DATA_STATE_0.equals(roleDto.getDataState())) {
            Integer count = userRoleMapper.countByRoleId(roleDto.getId());
            if (null != count && count > 0) {
                throw new BaseException(BasicEnum.ROLE_DISTRIBUTED);
            }
        }
        Boolean flag = roleMapper.update(bean);
        //重新构建角色和资源之间的关系 先删除 然后再判断前端是否传递了资源数据
        roleResourceMapper.deleteByRoleId(roleDto.getId() + "");
        if (ObjectUtil.isEmpty(roleDto.getCheckedDeptNos())) {
            //如果为空
            return flag;
        }
        List<RoleResource> roleResourceList = Lists.newArrayList();
        Arrays.asList(roleDto.getCheckedResourceNos()).stream().map(no ->{
            RoleResource roleResource = new RoleResource();
            roleResource.setRoleId(roleDto.getId());
            roleResource.setUpdateTime(LocalDateTime.now());
            roleResource.setCreateTime(LocalDateTime.now());
            roleResource.setResourceNo(no);
            roleResource.setDataState(SuperConstant.DATA_STATE_0);

            roleResourceList.add(roleResource);
            return roleResource;
        }).collect(Collectors.toList());
        roleResourceMapper.batchInsert(roleResourceList);
        return flag;
    }

    /**
     * 角色下拉框
     * @return
     */
    @Override
    public List<RoleVo> findAll() {
        return roleMapper.findAll();
    }
}
